//
//  ViewController.m
//  dd
//
//  Created by zc on 13-10-14.
//  Copyright (c) 2013年 zc. All rights reserved.
//

#import "ViewController.h"
#import "Model.h"
#import "Cell.h"
@interface ViewController ()
@property (strong,nonatomic)Model *model;
@property (strong,nonatomic)NSMutableArray *mAddCellArray;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    _model= [[Model alloc]initWithData];
    _tableView =[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    [self.view addSubview:_tableView];
    
    
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - tableview的数据源和委托
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_model.dataArray count];
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    id obj = [_model.dataArray objectAtIndex:[indexPath row]];
    if ([obj isKindOfClass:[NSDictionary class]]) {
        static NSString *CellIden=@"CustomCell";
        Cell *cell = [tableView dequeueReusableCellWithIdentifier:CellIden];
        if (cell==nil) {
            NSArray *array = [[NSBundle mainBundle]loadNibNamed:@"Cell" owner:self options:nil];
            cell = (Cell *)[array objectAtIndex:0];
        }
        cell.Name.text = [obj objectForKey:@"Name"];
        cell.Sex.text = [obj objectForKey:@"Sex"];
        return cell;
    }
    else{
        static NSString *cellIden2 =@"CustomCell2";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden2];
        if (cell==nil) {
            cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIden2];
        }
        cell.textLabel.text = obj;
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    id cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([cell isKindOfClass:[Cell class]]) {
        if ([cell bOpen]) {
            [self deleteCell:indexPath WithSender:cell];
        }
        else{
            [self addCell:indexPath WithSender:cell];
        }
    }
    
}

-(void)deleteCell:(NSIndexPath *)indexPath WithSender:(Cell *)cell;
{

    cell.bOpen=NO;
    NSMutableArray *indexPathArray= [[NSMutableArray alloc]init];
    for (int i=0; i<[_model.addArray count]; i++) {
        [_model.dataArray removeObjectAtIndex:indexPath.row+1 ];
        int index =indexPath.row+1+i;
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        
        [indexPathArray addObject:indexPath];
    }
    [_tableView beginUpdates];
    [_tableView deleteRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationLeft];
    [_tableView endUpdates];
    
    
    
}
-(void)addCell:(NSIndexPath *)indexPath WithSender:(Cell *)cell;
{

    cell.bOpen = YES;

    NSMutableArray *indexPathArray = [[NSMutableArray alloc]init];
    for (int i=0; i<[_model.addArray count]; i++) {
        NSString *phoneNumber = [_model.addArray objectAtIndex:i];
        
        int index = indexPath.row+1+i;
        
        [_model.dataArray insertObject:phoneNumber atIndex:index];
        NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:index inSection:0];
        [indexPathArray addObject:indexPath1];
        NSLog(@"indexPath %@ ----index %d -----indexPath1 %@",indexPath,index,indexPath1);
    }
    
    [_tableView beginUpdates];
    [_tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationRight];
    [_tableView endUpdates];
}





- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}


@end
